package com.ztesoft.huangfu.learning.springboot.springbootswagger2.controller;

import com.ztesoft.huangfu.learning.springboot.springbootswagger2.entity.ProcessResult;
import com.ztesoft.huangfu.learning.springboot.springbootswagger2.entity.Staff;
import com.ztesoft.huangfu.learning.springboot.springbootswagger2.entity.Student;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by codeya on 2017/12/5.
 */
@RestController
@RequestMapping("/staff")
@Slf4j
@Api(tags = "Staff相关服务")
public class StaffController {

    List list = new ArrayList();
    public StaffController() {
        for (int i = 0; i < 10000; i++) {
            Student student = new Student(i,"huangfu",1);
            list.add(student);
        }
    }


    @RequestMapping(value = "/test_path_variable/{id}")
    public ProcessResult test_path_variable(@PathVariable("id") String id) {
        log.info(id);
        ProcessResult result = new ProcessResult(ProcessResult.SUCCESS, "OK", "list=" + list);
        return result;
    }

    @RequestMapping(value = "/test_path_and_param/{id}")
    public ProcessResult test_path_and_param(@PathVariable("id") String id, @RequestParam("name") String name) {
        log.info(id.toString());
        ProcessResult result = new ProcessResult(ProcessResult.SUCCESS, "OK", "id=" + id + ",name=" + name);
        return result;
    }

    @RequestMapping(value = "/test_request_param")
    public ProcessResult test_request_param(@RequestParam("id") String id, @RequestParam("name") String name) {
        log.info("id:{},name:{}", id.toString(), name.toString());
        ProcessResult result = new ProcessResult(ProcessResult.SUCCESS, "OK", "id=" + id + "name=" + name);
        return result;
    }

    /**
     * post方式传递自定义对象。。ajax调用时需要json写法
     *
     * @return
     */
    @PostMapping("test_questbody")
    public ProcessResult test_questbody(@RequestBody Staff staff) {
        log.info(staff.toString());
        //staff.setBirthday(null);
        ProcessResult result = new ProcessResult(ProcessResult.SUCCESS, "OK", staff);
        return result;
    }

    @PostMapping("test_path_body/{id}")
    public ProcessResult test_path_body(@PathVariable("id") String id, @RequestBody Staff staff) {
        log.info("id" + id + ",staff:" + staff.toString());
        //staff.setBirthday(null);
        ProcessResult result = new ProcessResult(ProcessResult.SUCCESS, "OK", staff);
        return result;
    }

}
